home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf
/
Programming
/
Pascal_Tutes
/
Tute13.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-01-27
|
7KB
|
210 lines
{
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«» TUTORIAL THIRTEEN «»
«» «»
«»
by
«»
«» «»
«»
Anthony Peck
«»
«» «»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
Abandon scope all ye who enter...
Isn't this procedure stuff more trouble than it's worth? Nope,
but it does take some time to become familiar with some aspects of
procedural usage.
The weirdest thing is the concept of local and global variables.
Let's try to follow the logic of this next program, step by step...
* We identify a global variable as 'x'.
* At the start of the program 'x' is set to a value of '1'.
* We call the procedure 'first'.
* First identifies a local variable called 'x'.
* The local first variable 'x' is set to a value of '5'.
* First passes 'x' to the procedure 'second'.
* Second changes 'x' to '10' and passes it back to 'first'.
* Back in the main program, the global variable 'x' still
value of '1'.
* The main program passes 'x' to the procedure 'second'.
* Second changes 'x' to '10' and passes it back to the main
program.
Phew!!
Check out diagram_five for the complete "picture". This is an
animated version of the above flow chart. Thanks to Chas Wyndham
for S_Anim5 (found on MD41).
}
PROGRAM MDTute13;
{ Program demonstrates local and global variables
Author : A N Peck
Date : 28 October 1994
Procedures used:
First - defines local identifier x
Second - defines local variable x }
Var x: byte;
{
^
|
|__
The global variable "x" is defined.
}
{ ---------------------------------------------------------- }
Procedure second(Var x: Byte);
{
^
|
|__
This procedure is passed a variable which now
can be changed by the procedure. If the
"Var" was missing, what do you think
would happen?
}
Begin
x := 10;
{
^
|
|__
This becomes a permanent change because 'x' was passed as a
"Var". Isn't this just wonderful?
}
Writeln;
Writeln(' In Procedure second x equals ',x);
Writeln;
End; { second }
{ ---------------------------------------------------------- }
Procedure first(x:byte);
{
^
|
|__
This procedure identifies x as the byte value
passed to it, but it cannot alter the global
variable 'x'.
}
Begin
x := 5;
{
^
|
|__
This initialisation of 'x' is only valid within this
procedure, and those procedures which are called from here.
This does not affect the global variable 'x'. If you think
this is complicated, you should see my family tree!
}
Writeln;
Writeln(' In Procedure first, before calling second, x equals ',x);
second(x);
{
^
|
|__
"Second" is called, and 'x' is passed to the procedure.
}
Writeln(' In Procedure first, after calling second, x equals ',x);
Writeln;
End; { first }
{ ---------------------------------------------------------- }
{ ---------------------------------------------------------- }
Begin { Main Program }
x := 1;
{
^
|
|__
Initialises global variable x as '1'
}
Writeln;
Writeln(' Before calling any Procedures x equals ',x);
first(x);
{
^
|
|__
'x' is passed to the "first "procedure.
}
Writeln(' After calling first from the main program x equals ',x);
second(x);
{
^
|
|__
'x' is passed to "second", but this time it can be altered by
the procedure.
}
Writeln(' After calling second from the main program x equals ',x);
Writeln;
End. { Main Program }
{ ---------------------------------------------------------- }
{
Ergonomic! Check out the animation again, and try to follow
through the reasoning. Of course, no idiot would define all the
variables within a program as 'x', but it does illustrate the point
that the compiler is often a wee bit smarter than the compilee!
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«»
cyclops quod erat demonstrandum
«»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
}